package main

import (
	"context"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	docs "org.knight/my-workbench/docs"

	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/unidoc/unioffice/common/license"
	adapter "org.knight/adapter/http"
	"org.knight/biz"
)

func init() {
	// Make sure to load your metered License API key prior to using the library.
	// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
	err := license.SetMeteredKey("9190d06078ff94a55e85315c57e221f45f1e3e3b0689cb9aa98877986cf82121")
	if err != nil {
		panic(err)
	}
}

// type Label struct {
// 	Charpter int32
// 	Unit     int32
// 	Section  int32
// 	Item     string
// }

// type IE struct {
// 	Label Label
// 	Name  string
// 	Desc  string
// }

func main0() {
	log.Println("main go..")
	parser := biz.DocParser{}
	log.Println("try init...")
	parser.Init()
	log.Println("trying to parse")
	parser.Parse("d:\\24501.docx")
	log.Println("Done parse")
}

// @title           Swagger Example API
// @version         1.0
// @description     This is a sample server celler server.
// @termsOfService  http://swagger.io/terms/

// @contact.name   API Support
// @contact.url    http://www.swagger.io/support
// @contact.email  support@swagger.io

// @license.name  Apache 2.0
// @license.url   http://www.apache.org/licenses/LICENSE-2.0.html

// @host      localhost:8080
// @BasePath  /api/v1

func main() {

	router := gin.Default()
	httpHanlder := adapter.IEHttpHandler{}
	specFileHandler := adapter.SpecFileHttpHandler{}
	// router.GET("/infoElement", func(ctx *gin.Context) {
	// 	httpHanlder.Handle(ctx)
	// })

	docs.SwaggerInfo.BasePath = "/api/v1"
	v1 := router.Group("/api/v1")

	{

		v1.GET("/infoElement", httpHanlder.Handle)

		v1.POST("/upload/3gpp/spec", specFileHandler.Handle)
	}

	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	// router.POST("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	// router.DELETE("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	// router.PUT("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

	svr := &http.Server{
		Addr:    ":8080",
		Handler: router,
	}

	go func() {
		if err := svr.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("Failed listen %s \n", err)
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	log.Println("shutdown server")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

	defer cancel()

	if err := svr.Shutdown(ctx); err != nil {
		log.Fatal("Server shutdown", err)
	}

	log.Println("server existing")

}
